package edu.ruc.cluster.test;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import edu.ruc.cluster.util.DefaultLogHandler;

/**
 * 2012-12-13
 * Used: a self-made Dialog, call showDialog, ScheduledExecutorService to run something and exit automatically
 * Called Example:
 * 		TimeDialog timer = new TimeDialog();
 * 		timer.showDialog(new JFrame(), message, seconds);
 * Change the s.schedule.run() for customized needs around line 66
 * @author deke
 */
public class TimeDialog {
//	private String message = null;
//    private int secends = 0;
    private JLabel label = new JLabel();
    private JButton confirm;
//    private JButton cancel; 
    private JDialog dialog = null;
    int result = -5;
    private DefaultLogHandler logHandler = DefaultLogHandler.getInstance();
    
    public int showDialog(JFrame father, String message, int sec) {
    	
//        this.message = message;
//        secends = sec;
        label.setText(message);
        label.setBounds(80,6,200,20);
        ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
        
        
        confirm = new JButton("ok");
        confirm.setBounds(100,40,60,20);
        confirm.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				result = 0;
				TimeDialog.this.dialog.dispose();
			}
		});
		
        /*
        cancel = new JButton("Cancel");
        cancel.setBounds(190,40,60,20);
        cancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				result = 1;
				TimeDialog.this.dialog.dispose();
			}
		});
		*/
        dialog = new JDialog(father, true);
        dialog.setTitle("Notice");
        dialog.setLayout(null);
        dialog.add(label);
        dialog.add(confirm);
//        dialog.add(cancel);
        s.schedule(new Runnable(){
        	@Override
            public void run() {
                // TODO Auto-generated method stub
        		logHandler.info("going to run configuration......");
//            	Entrance entrance = new Entrance();
//            	entrance.Run();
//            	TimeDialog.this.dialog.dispose();
        	}
        }, 1, TimeUnit.SECONDS);
        
        /*
        s.scheduleAtFixedRate(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
            	Entrance entrance = new Entrance();
            	entrance.Run();
            	TimeDialog.this.dialog.dispose();
               
//                TimeDialog.this.secends--;
//                if(TimeDialog.this.secends == 0) {
//                    TimeDialog.this.dialog.dispose();
//                }else {
//                	dialog.setTitle("Notice: This windos is going to close in "+secends+" seconds");
//                }
            
            }
        }, 1, 1, TimeUnit.SECONDS);
        
        */
        dialog.pack();
        dialog.setSize(new Dimension(350,100));
        dialog.setLocationRelativeTo(father);
        dialog.setVisible(true);
        return result;
    }
    public static void main(String[] args){
    	TimeDialog timer = new TimeDialog();
    	timer.showDialog(new JFrame(), "running...", 5);
    }
}
